define(window.backboneCommon.getViewDefine("crowd.index"), function ($,require,Collection,tpl,ViewCustom,ViewSimilar) {
    var backboneView= Backbone.View.extend({
        events: {
            "click .add-info": "addInfo",
            "click .edit-info": "editInfo"
        },
        initialize: function () {
            this.modelAttach={};
            this.modelAttach.customer=this.model.get("modelCustomer");
            this.listenTo(this.modelAttach.customer, 'bind:AllList', this.bindCustomer);
            this.listenTo(this.model, 'bind:PageList', this.bindList);
            this.listenTo(this.model, 'delete:Info', this.deleteInfoCallback);
            this.render();
        },
        render:function(){
            var templateHtml=_.template(tpl);
            this.$el.html(templateHtml());
            return this;
        },
        initMain:function(){
            this.elementModel={};
            this.extObject={};
            this.pageData={isRefreshSimilar:true};
            this.elementModel.table=$("#tb_list");
            this.initViewAttach();
            this._initPagePower();
        },
        _initPagePower:function(){
            var self=this;
            $.ExtAssist.rolePower.operatePage("list",["advertiserList"],function(json){
                self.pageData.powerInfo=json;
                self._initBodyMain();
                if(json["advertiserList"]){self.collection.getCustomer()}
            });
        },
        _initBodyMain:function(){
            this._initInputSearch();
            this._initPageList();
        },
        initViewAttach:function () {
            var self=this;
            self.viewAttach={};
            self.viewAttach.custom=new ViewCustom({pElement:"#container_customInfo",
                submitCallback:function (isAdd,json) {
                    self.pageData.isRefreshSimilar=isAdd;
                    self.submitInfo(isAdd,json)
                }
            });
            self.viewAttach.similar=new ViewSimilar({
                pElement: "#container_similarInfo",
                addCustomCallback: function () {
                    self.viewAttach.custom.init()
                },
                submitCallback: function (isAdd, json) {
                    self.submitInfo(isAdd, json)
                }
            });
        },
        _initInputSearch:function () {
            var self=this;
            self.extObject.inputSearch=$("#panel_inputSearch").ExtInputSearch({
                placeholder:"人群ID,人群名称",
                searchCallback:function () {
                    self.refreshList();
                }
            })
        },
        bindCustomer:function () {
            var self=this;
            var res=this.modelAttach.customer.get("dataAllList");
            var list=$.ExtCommon.convertListByPara(res.data,{name:"nameExt"});
            $("#search_customer").ExtSelect2({placeholder: "-广告主-",allowClear: true,minimumResultsForSearch: 1,infoExt:{data:list,
                changeCallback:function(){self.refreshList()}
            }});
        },
        _getSearchParam:function(){
            var param={};
            var keyword = this.extObject.inputSearch.getValue();
            if (!$.ExtCommon.isCheckEmpty(keyword)) {param.search = keyword;}
            var customer=$("#search_customer").val();
            if (!$.ExtCommon.isCheckEmpty(customer)) {param.customer = customer;}
            return param;
        },
        _initPageList: function () {
            var self=this;
            var options={
                columns: [
                    {data: "id",class: "w-60"},
                    {data: "name",class: "w-min-100"},
                    {data: "customerName",class: "w-200"},
                    {data: "typeName",class: "w-80 text-center"},
                    {data: "createTime", class: "w-140"},
                    {data: "statusName", class: "w-60 text-center"},
                    {data: "coverCount", class: "w-85 num-int"},
                    {data: null, class: "w-50 text-center"}
                ],
                infoExt:{
                    ajaxDataPara:function(){
                        var param= {};
                        param= $.ExtCommon.initMergeJson(param,self._getSearchParam());
                        return param;
                    },
                    ajaxWholeCallback:function(param){
                        self.collection.getPageList(param)
                    }
                },
                "createdRow": function (row, data) {
                    var elementTd=$('td', row);
                    elementTd.eq(0).attr("id","crowd_"+data.id);
                    var isPowerCustomer=self.pageData.powerInfo["advertiserList"];
                    var indexOperate=isPowerCustomer?7:6;
                    var type="";
                    if(data.isCustom){type="custom"}
                    if(data.isSimilar){type="similar"}
                    elementTd.eq(indexOperate).html($.ExtCommon.getStringConnect('<a class="btn-operate longterm edit-info" data-type="{0}" href="javascript:">编辑</a>',[type]));
                },
                "drawCallback": function () {
                    $.ExtNumeric.initAll();
                    $.ExtAssist.urlParam.setUrl();
                }
            };
            options.columns[2]["visible"]=self.pageData.powerInfo["advertiserList"];
            self.extObject.table=self.elementModel.table.ExtDataTable(options);
        },
        bindList:function(){
            var data=this.model.get("dataPageList");
            this.extObject.table.ajaxWholeRefreshData(data);
        },
        refreshList: function () {this.extObject.table.refresh()},

        addInfo:function (e) {
            var type=$(e.currentTarget).data("type");
            if(type==="custom"){
                this.viewAttach.custom.init();
            }else{
                this.viewAttach.similar.init(null,{isInitList:this.pageData.isRefreshSimilar});
                this.pageData.isRefreshSimilar=false;
            }
        },
        editInfo:function (e) {
            var tr = $(e.target).parents("tr:first");
            var rowData = this.extObject.table.getRowInfo(tr).data();
            var type=$(e.currentTarget).data("type");
            this.viewAttach[type].init(rowData.id);
        },
        submitInfo:function (isAdd,info) {
            if(isAdd){
                this.refreshList();
            }else{
                var tr=$("#crowd_"+info.id).parent();
                var row = this.extObject.table.getRowInfo(tr);
                var elementTd=$('td', tr);
                if(!$.ExtCommon.isCheckEmpty(info.name)){
                    row.data()["name"]=info.name;
                    elementTd.eq(1).html(info.name)
                }
                if(!$.ExtCommon.isCheckEmpty(info.customerName)){
                    row.data()["customerName"]=info.customerName;
                    elementTd.eq(2).html(info.customerName)
                }
            }
        }
    });
    var viewModel=function(){
        this.collection= new Collection();
        this.model=this.collection.model;
        this.view=new backboneView({el: "#container_body",collection:this.collection,model:this.model});
    };
    viewModel.prototype={
        initView:function () {
            this.view.initMain();
        }
    };
    return viewModel;
});